package com.fancy.mobileschool.service;


import android.util.Log;

import com.fancy.mobileschool.MyConfiguration;
import com.fancy.mobileschool.entity.Attendance;
import com.fancy.mobileschool.entity.AttendanceResult;
import com.fancy.mobileschool.util.GsonUtil;
import com.google.gson.Gson;

import java.util.List;

import okhttp3.FormBody;



/**
 * Created by Jackie on 2017/11/22.
 *
 */

public class AttendService {

    private static final String baseUrl = URLConfig.baseUrl +  "attendance";

    //private static final String baseUrl1 =  "http://120.78.197.155:8080/MobileSchool-chat/attendance";

    private Gson gson = GsonUtil.getGson();

    public Attendance releaseAttendance(Attendance[] attendance) {
        Attendance rs = null;
        try {
            FormBody body = new FormBody.Builder()
                    .add("attendance", gson.toJson(attendance[0]))
                    .add("myID", MyConfiguration.myID).build();

            String url = baseUrl + "/addAttendance.do";
            rs = OKHttpBase.fromJsonByPost(url, body, Attendance.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }

    public AttendanceResult studentAttend(AttendanceResult[] attendanceResult) {
        AttendanceResult rs;

        String url = baseUrl + "/studentAttend.do";
        FormBody body = new FormBody.Builder()
                    .add("body", gson.toJson(attendanceResult[0]))
                    .build();

        rs = OKHttpBase.fromJsonByPost(url, body, AttendanceResult.class);
        return rs;
    }

    public List<Attendance> getAttendancesForTeacher(String teacherID) {
        List<Attendance> rs;
        String url = baseUrl + "/getAttendancesForTeacher.do?myID=" + teacherID;
        rs = OKHttpBase.fromJsonListByGet(url, Attendance.class);
        return rs;
    }


    public List<AttendanceResult> getStudentAttendsResult(String courseID, String day) {

        List<AttendanceResult> rs;
        String getURL = baseUrl + "/getStudentAttendsResult.do?courseID=" + courseID + "&day=" + day;
        Log.i("wode", getURL);
        rs = OKHttpBase.fromJsonListByGet(getURL, AttendanceResult.class);
        Log.i("wode", rs.toString());
        return rs;

    }

}
